package org.ipower.examsystem.service.impl;

import java.util.List;
import org.ipower.examsystem.dao.MembershipCatalogDao;
import org.ipower.examsystem.dao.MembershipDao;
import org.ipower.examsystem.domain.Membership;
import org.ipower.examsystem.domain.MembershipCatalog;
import org.ipower.examsystem.service.MembershipCatalogInfoCollection;
import org.ipower.examsystem.service.MembershipService;
import org.ipower.platform.domain.Pager;
import org.ipower.platform.domain.QueryCondition;

/**
 * 会员管理服务实现类。
 * @author jeasonyoung
 */
public class MembershipServiceImpl implements MembershipService {
    private static final long serialVersionUID = 1L;
    private MembershipDao dao = null;
    private MembershipCatalogDao mcdao = null;
   
    @Override
    public void setMembershipDao(MembershipDao dao) {
        this.dao = dao;
    }
  
    @Override
    public void setMembershipCatalogDao(MembershipCatalogDao dao) {
        this.mcdao = dao;
    }
     
    @Override
    public Pager<Membership> findAll(String ascProperty, boolean isAsc, int start, int limit, List<QueryCondition> entites) {
        return this.dao.findAll(ascProperty, isAsc, start, limit, entites);
    }
   
    @Override
    public void update(Membership data, List<MembershipCatalog> list) {
      if(data != null && data.getMemberID() != null && !data.getMemberID().isEmpty()){
         this.mcdao.delete("MemberID", data.getMemberID());
         this.dao.update(data);
         if(list != null && list.size() > 0){
             for(MembershipCatalog mc:list){
                 if(mc != null && mc.getMemberID() != null && !mc.getMemberID().isEmpty() && mc.getCatalogID() != null && !mc.getCatalogID().isEmpty()){
                     this.mcdao.save(mc);
                 }
             }
         }
      }
    }

    @Override
    public MembershipCatalogInfoCollection createCatalogTree(String memberID) {
        return this.mcdao.createCatalogTree(memberID);
    }

    @Override
    public void delete(String memberID) {
         if(memberID != null && !memberID.isEmpty()){
             this.mcdao.delete("MemberID", memberID);
             this.dao.delete(memberID);
         }
    }
}